package com.dhx.dao.impl;

import com.dhx.dao.LeaveStudentDao;
import com.dhx.pojo.LeaveStudent;
import com.dhx.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LeaveStudentDaoImpl implements LeaveStudentDao {
    @Override
    public int addInfo(LeaveStudent student) {
        String sql = "insert into t_leavestudent values (?,?,?,?,?,?,?,?)";
        Object[] objs = {student.getName(), student.getNo(), student.getMajor(), student.getSex(), student.getLevel(), student.getAge(), student.getDate(), student.getSchool()};
        return DBUtil.excuteDML(sql, objs);
    }

    @Override
    public int deleteInfoById(String no) {
        String sql = "delete from t_leavestudent where no=?";
        Object[] objs = {no};
        return DBUtil.excuteDML(sql, objs);
    }

    @Override
    public int updateInfo(LeaveStudent student) {
        String sql = "update t_leavestudent set name=?,major=?,sex=?,level=?,age=?,date=?,school=? where no=?";
        Object[] objs = {student.getName(), student.getMajor(), student.getSex(), student.getLevel(), student.getAge(), student.getDate(), student.getSchool(), student.getNo()};
        return DBUtil.excuteDML(sql, objs);
    }

    @Override
    public LeaveStudent queryInfoById(String no) {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from t_leavestudent where no=?";
        PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
        try {
            ps.setString(1, no);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        ResultSet rs = null;
        LeaveStudent leaveStudent = null;
        try {
            rs = ps.executeQuery();
            if (rs.next()) {
                leaveStudent = new LeaveStudent(rs.getString("name"), rs.getString("no"), rs.getString("major"), rs.getString("sex"), rs.getString("level"), rs.getInt("age"), rs.getDate("date"), rs.getString("school"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, ps, conn);
        }
        return leaveStudent;
    }

    @Override
    public List<LeaveStudent> queryInfoes() {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from t_leavestudent";
        PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
        ResultSet rs = null;
        List<LeaveStudent> list = new ArrayList<LeaveStudent>();
        try {
            rs = ps.executeQuery();
            while (rs.next()) {
                LeaveStudent leaveStudent = new LeaveStudent(rs.getString("name"), rs.getString("no"), rs.getString("major"), rs.getString("sex"), rs.getString("level"), rs.getInt("age"), rs.getDate("date"), rs.getString("school"));
                list.add(leaveStudent);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, ps, conn);
        }
        return list;
    }
}
